// pages/destination/views/detail/detail.js
const app = getApp()
const http = require("../../../../utils/http.js")


Page({

  /**
   * 页面的初始数据
   */
  data: {
    ScreenHeight: app.globalData.ScreenHeight,
    WindowHeight: app.globalData.WindowHeight,
    mddId: '',
    isAttention: false,
    mdd: {},
    pois: [],
    hasUserInfo: app.globalData.hasUserInfo,
    openid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
    self.setData({
      mddId: options.mddId,
      hasUserInfo: app.globalData.hasUserInfo
    })
    let param = {
      mddId: options.mddId
    }
    if (app.globalData.hasUserInfo){
      self.setData({
        openid: wx.getStorageSync("tokenId")
      })
      param.openid = self.data.openid
    }
    http.get("/mdd/getDetail", param).then(res=>{
      self.setData({
        isAttention: res.data.data.isAttention,
        mdd: res.data.data.mdd,
        pois: res.data.data.pois
      })
    })
  },

  addOrCancel(){
    console.log(this.data.hasUserInfo)
    if (!this.data.hasUserInfo){
      wx.showToast({
        title: '请先登录账号',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    let param = {
      mddId: this.data.mdd.mddId,
      openid: this.data.openid
    }
    let url = "/attention/add"
    let bool = this.data.isAttention;
    if (bool){
      url = "/attention/cancel"
    }
    http.post(url, param).then(res => {
      if (res.data.status == 200){
        this.setData({
          isAttention: !bool
        })
        wx.showToast({
          title: res.data.message,
          icon: 'success',
          duration: 1500
        })
      }else{
        wx.showToast({
          title: res.data.message,
          icon: 'fail',
          duration: 1500
        })
      }
    })
    
  },

  morePoi(){
    wx.navigateTo({
      url: '/pages/location/more/more?mddId=' + this.data.mddId,
    })
  },

  toPoiDetail(e){
    wx.navigateTo({
      url: '/pages/location/poidetail/poidetail?poiId=' + e.currentTarget.dataset.poiid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})